package com.si.anddos;

import com.si.anddos.dpadapters.AnddosDbAdapter;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

/**
 * A class representing detail of individual upload file
 * 
 * @author Carlos Martinez
 * 
 */
public class UploadDetailActivity extends OptionsMenuActivity {
	private AnddosDbAdapter dbAdapter;
	
	private long uploadId;

	private TextView textPath;
	private Spinner spinnerUploadDetailAccounts;

	private Button buttonSave;
	
	private TextView textStatus;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.upload_detail);
		setTitle(R.string.titleDetailUpload);

		Bundle extras = getIntent().getExtras();
		uploadId = extras != null ? extras.getLong(AnddosDbAdapter.colUploadID)
				: null;

		textPath = (TextView) findViewById(R.id.textPath);
		spinnerUploadDetailAccounts = (Spinner) findViewById(R.id.spinnerUploadDetailAccounts);

		final ToggleButton buttonStartStop = (ToggleButton) findViewById(R.id.buttonUploadDetailStartStop);
		buttonStartStop.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				if (buttonStartStop.isChecked()) {
					setResult(RESULT_OK);
					Toast.makeText(view.getContext(), "Not implemented yet",
							Toast.LENGTH_LONG).show();
				} else {
					setResult(RESULT_OK);
					Toast.makeText(view.getContext(), "Not implemented yet",
							Toast.LENGTH_LONG).show();
				}

			}
		});

		// on click listener for button save
		buttonSave = (Button) findViewById(R.id.buttonUploadDetailSave);
		buttonSave.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				save();
				setResult(RESULT_OK);
				finish();
			}
		});
		
		dbAdapter = new AnddosDbAdapter(this);
		dbAdapter.open();
	}
	
	private String getStatusString(int status) {
    	if (status == AnddosDbAdapter.StatusWaiting) {
    		this.textStatus.setTextColor(0xffffff00);
    		return " Waiting";
    	} else if (status == AnddosDbAdapter.StatusProcessing) {
    		this.textStatus.setTextColor(0xff0000ff);
    		return " Processing";
    	} else if (status == AnddosDbAdapter.StatusError) {
    		this.textStatus.setTextColor(0xffff0000);
    		return " Error";
    	} else if (status == AnddosDbAdapter.StatusDone) {
    		this.textStatus.setTextColor(0xff00ff00);
    		return " Done";
    	} else if (status == AnddosDbAdapter.StatusStopped) {
    		this.textStatus.setTextColor(0xff888888);
    		return " Stopped";
    	} else {
    		return "";
    	}
	}
	
	//nahrada za tlacitko cancel
	@Override
	public void onBackPressed() {
		setResult(RESULT_OK);
		finish();
	}
	
	protected void onResume() {
		super.onResume();
		// populate upload detail from db to activity
		fillData();
	}

	private void fillData() {
		dbAdapter.open();
		Cursor uploadDetail = dbAdapter.getUpload(uploadId);
		startManagingCursor(uploadDetail);
		
		String path = uploadDetail.getString(uploadDetail
				.getColumnIndexOrThrow(AnddosDbAdapter.colUploadFilepath));
		textPath.setText(path);
		
		Cursor account = dbAdapter.getAllAccounts();
		startManagingCursor(account);
		
		// vytvorime pole s hodnotami, ktere chceme zobrazit
		String[] from = new String[] { AnddosDbAdapter.colAccountUsername,
				AnddosDbAdapter.colAccountService };

		int[] to = new int[] { R.id.textAccountRow, R.id.textAccountRowService };

		spinnerUploadDetailAccounts.setAdapter(new SimpleCursorAdapter(this,
				R.layout.account_list_row, account, from, to));

		long detailAccount = uploadDetail.getLong(uploadDetail
				.getColumnIndexOrThrow(AnddosDbAdapter.colDownloadAccount));

		for (int i = 0; i < spinnerUploadDetailAccounts.getCount(); i++) {
			Cursor value = (Cursor) spinnerUploadDetailAccounts
					.getItemAtPosition(i);
			long id = value.getLong(value.getColumnIndexOrThrow("_id"));
			if (id == detailAccount) {
				spinnerUploadDetailAccounts.setSelection(i);
			}
		}

		int status = uploadDetail.getInt(uploadDetail
				.getColumnIndexOrThrow(AnddosDbAdapter.colUploadStatus));
		textStatus = (TextView)findViewById(R.id.textUploadStatusText);
		textStatus.setText(this.getStatusString(status));

		// pokud nejsou v DB ucty, tak skryje spinner
		if (account.getCount() == 0)
			spinnerUploadDetailAccounts.setEnabled(false);
		dbAdapter.close();
	}
	
	private void save() {
		dbAdapter.open();
		Cursor uploadDetail = dbAdapter.getUpload(uploadId);
		startManagingCursor(uploadDetail);
		
		String uploadId =uploadDetail.getString(uploadDetail
				.getColumnIndexOrThrow(AnddosDbAdapter.colUploadID));
		int id = Integer.parseInt(uploadId);
		
		String fileName = uploadDetail.getString(uploadDetail
				.getColumnIndexOrThrow(AnddosDbAdapter.colUploadFilename));
		
		String filePath = uploadDetail.getString(uploadDetail
				.getColumnIndexOrThrow(AnddosDbAdapter.colUploadFilepath));
		
		String statusString = uploadDetail.getString(uploadDetail
				.getColumnIndexOrThrow(AnddosDbAdapter.colUploadStatus));
		int status = Integer.parseInt(statusString);
		
		long accountId = spinnerUploadDetailAccounts.getSelectedItemId();

		// save change of upload to database
		dbAdapter.updateUpload(id, fileName, filePath, status, accountId);
		dbAdapter.close();
	}
}
